from PySide2.QtCore import QSize, QEvent, QPointF, SIGNAL, SLOT, QByteArray
from PySide2.QtNetwork import QUdpSocket
from PySide2.QtWidgets import QApplication, QLabel, QFileDialog, QWidget, QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2.QtGui import Qt, QPixmap, QImage, QPen, QColor, QPainter
from os.path import join
import psutil, glob, os, datetime, cv2, numpy
import 图标

class Stats(QMainWindow):
    def __init__(self):
        super(Stats, self).__init__()
        # 从文件中加载UI定义

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意：里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('untitled.ui')

        if os.path.exists('./Data'):
            if os.path.exists('./Data/image') == False:
                os.mkdir('./Data/image')
            if os.path.exists('./Data/photo') == False:
                os.mkdir('./Data/photo')
            if os.path.exists('./Data/video') == False:
                os.mkdir('./Data/video')
            if os.path.exists('./Data/Transform') == False:
                os.mkdir('./Data/Transform')
        else:
            os.mkdir('./Data')
            os.mkdir('./Data/image')
            os.mkdir('./Data/photo')
            os.mkdir('./Data/video')
            os.mkdir('./Data/Transform')

        self.RxNum = 0
        self.RxCompleteFlag = 0
        self.UdpCache = QByteArray()
        self.ui.ip_lable = QLabel()
        self.ui.RxNum = QLabel()
        self.ui.ip_refresh = QLabel()
        self.ui.statusbar.addPermanentWidget(self.ui.ip_lable)
        self.ui.statusbar.addPermanentWidget(self.ui.RxNum)
        self.ui.RxNum.setText('当前接收图片量:' + str(self.RxNum))
        self.ui.statusbar.addWidget(self.ui.ip_refresh)
        self.RxSign = False
        self.ImageSaveSign = False

        self.WifiConnectCheck()
        self.ui.pushButton_2.clicked.connect(self.WifiRefresh)
        self.ui.pushButton_3.clicked.connect(self.StatsChildShow)
        self.ui.pushButton.clicked.connect(self.RxSettings)

    def WifiRefresh(self):
        info = psutil.net_if_stats()
        if info['WLAN'][0]==False:
            self.ui.ip_lable.setText('未连接Wifi')
            self.ui.ip_refresh.setText('请连接wifi后刷新IP地址')
        else:
            ip = self.GetHostIp()
            self.ui.ip_lable.setText('本机IP地址:'+ip)
            self.ui.ip_refresh.setText('刷新本机IP地址为:'+ip)

    def WifiConnectCheck(self):
        info = psutil.net_if_stats()
        if info['WLAN'][0]==False:
            self.ui.ip_lable.setText('未连接Wifi')
            self.ui.ip_refresh.setText('请连接wifi后刷新IP地址')
        else:
            ip = self.GetHostIp()
            self.ui.ip_lable.setText('本机IP地址:'+ip)

    def GetHostIp(self):
        info = psutil.net_if_addrs()
        ip = info['WLAN'][1][1]
        return ip

    def StatsChildShow(self):
        self.StatsChild=StatsChild()
        self.StatsChild.ui.show()

    def initSocket(self):
        self.udpSocket = QUdpSocket()
        self.udpSocket.bind(8080)

        self.connect(self.udpSocket, SIGNAL('readyRead()'),
                     self, SLOT('readPendingDatagrams()'))

    def RxSettings(self):
        if self.RxSign == False:
            if self.ui.lineEdit.text() != '' and self.ui.lineEdit_2.text() != '':
                self.ImageHeight = int(self.ui.lineEdit.text())
                self.ImageWidth = int(self.ui.lineEdit_2.text())
                if self.ui.checkBox.isChecked() == True:
                    i = datetime.datetime.now()
                    self.filename = i.strftime("%Y-%m-%d %H-%M-%S")
                    os.mkdir('./Data/image/' + self.filename)
                    self.ImageSaveSign = True
                else:
                    self.ImageSaveSign = False
                self.RxSign = True
                self.ui.pushButton.setText('停止接收')
                self.initSocket()
                self.ui.label_4.installEventFilter(self)
                self.ui.ip_refresh.setText('开始接收')
        else:
            self.RxSign = False
            self.ui.pushButton.setText('开始接收')
            self.disconnect(self.udpSocket, SIGNAL('readyRead()'),
                            self, SLOT('readPendingDatagrams()'))
            self.RxNum = 0
            self.ui.RxNum.setText('当前接收图片量:' + str(self.RxNum))
            self.ui.ip_refresh.setText('停止接收')

    def readPendingDatagrams(self):
        while self.udpSocket.hasPendingDatagrams():
            datagram = QByteArray()
            datagram.resize(self.udpSocket.pendingDatagramSize())
            (datagram, sender, senderPort) = self.udpSocket.readDatagram(datagram.size())
            if datagram == b'\x00\xff\x01\x01':
                self.UdpCache.clear()
            elif datagram == b'\x01\x01\xff\x00':
                self.RxCompleteFlag = 1
                print(len(self.UdpCache))
            else:
                self.UdpCache.append(datagram)

        if self.ui.checkBox_3.isChecked() == False:
            if self.RxCompleteFlag == 1 and len(self.UdpCache)>=int((self.ImageHeight * self.ImageWidth) / 8):
                binarray = QByteArray()
                if self.ui.checkBox_2.isChecked() == False:
                    for i in range(0, int((self.ImageHeight * self.ImageWidth) / 8)):
                        for j in range(0, 8):
                            if self.UdpCache.data()[i] & (0x01 << (7 - j)):
                                binarray.append(b'\xff')
                            else:
                                binarray.append(b'\x00')
                else:
                    for i in range(0, int((self.ImageHeight * self.ImageWidth) / 8)):
                        for j in range(0, 8):
                            if self.UdpCache.data()[i] & (0x01 << (7 - j)):
                                binarray.append(b'\x00')
                            else:
                                binarray.append(b'\xff')

                self.image = QImage(binarray, self.ImageWidth, self.ImageHeight, QImage.Format_Indexed8)
                self.pixmap = QPixmap.fromImage(self.image)

                if self.ui.checkBox_4.isChecked() == True:
                    if self.UdpCache.size() > int((self.ImageHeight * self.ImageWidth) / 8):
                        painter = QPainter(self.pixmap)
                        painter.setPen(QPen(QColor(255, 0, 0)))
                        for i in range(0, self.ImageHeight):
                            painter.drawPoint(QPointF(self.UdpCache.data()[int((self.ImageHeight * self.ImageWidth) / 8) + i], i))
                        painter.setPen(QPen(QColor(0, 255, 0)))
                        for i in range(0, self.ImageHeight):
                            painter.drawPoint(QPointF(self.UdpCache.data()[int((self.ImageHeight * self.ImageWidth) / 8) + self.ImageHeight + i], i))

                self.pixmapZoom = self.pixmap.scaled(self.ImageWidth * 5, self.ImageHeight * 5, Qt.IgnoreAspectRatio)

                if self.ImageSaveSign == True:
                    self.pixmapZoom.save('./Data/image/' + self.filename + '/' + str(self.RxNum) + '.jpg', "JPG")

                self.ui.label_4.setPixmap(self.pixmapZoom)
                self.ui.label_4.setMaximumWidth(self.pixmapZoom.width())
                self.ui.label_4.setMaximumHeight(self.pixmapZoom.height())
                self.ui.label_4.resize(QSize(self.pixmapZoom.width(), self.pixmapZoom.height()))
                self.RxNum = self.RxNum + 1
                self.ui.RxNum.setText('当前接收图片量:' + str(self.RxNum))
                self.RxCompleteFlag = 0
        else:
            if self.RxCompleteFlag == 1 and len(self.UdpCache) >= int(self.ImageHeight * self.ImageWidth):
                self.image = QImage(self.UdpCache, self.ImageWidth, self.ImageHeight, QImage.Format_Indexed8)
                self.pixmap = QPixmap.fromImage(self.image)

                if self.ui.checkBox_4.isChecked() == True:
                    if self.UdpCache.size() > int(self.ImageHeight * self.ImageWidth):
                        painter = QPainter(self.pixmap)
                        painter.setPen(QPen(QColor(255, 0, 0)))
                        for i in range(0, self.ImageHeight):
                            painter.drawPoint(QPointF(self.UdpCache.data()[int((self.ImageHeight * self.ImageWidth)) + i], i))
                        painter.setPen(QPen(QColor(0, 255, 0)))
                        for i in range(0, self.ImageHeight):
                            painter.drawPoint(QPointF(self.UdpCache.data()[int((self.ImageHeight * self.ImageWidth)) + self.ImageHeight + i], i))

                self.pixmapZoom = self.pixmap.scaled(self.ImageWidth * 5, self.ImageHeight * 5, Qt.IgnoreAspectRatio)

                if self.ImageSaveSign == True:
                    self.pixmapZoom.save('./Data/image/' + self.filename + '/' + str(self.RxNum) + '.jpg', "JPG")

                self.ui.label_4.setPixmap(self.pixmapZoom)
                self.ui.label_4.setMaximumWidth(self.pixmapZoom.width())
                self.ui.label_4.setMaximumHeight(self.pixmapZoom.height())
                self.ui.label_4.resize(QSize(self.pixmapZoom.width(), self.pixmapZoom.height()))
                self.RxNum = self.RxNum + 1
                self.ui.RxNum.setText('当前接收图片量:' + str(self.RxNum))
                self.RxCompleteFlag = 0

    def eventFilter(self, watched, event):
        if watched == self.ui.label_4:
            if event.type() == QEvent.MouseButtonPress:
                if event.button() == Qt.LeftButton:
                    self.pixmapZoom.save('./Data/photo/' + str(self.RxNum) + '.jpg', "JPG")
                    self.ui.ip_refresh.setText(str(self.RxNum) + '.jpg已保存')
                return True
            return super(Stats, self).eventFilter(watched, event)

class StatsChild(QWidget):
    def __init__(self):
        super(StatsChild, self).__init__()
        self.ui = QUiLoader().load('untitledChild.ui')

        self.imageScale = 1
        self.ui.left_label = QLabel()
        self.ui.right_label = QLabel()
        self.ui.statusbar.addWidget(self.ui.left_label)
        self.ui.statusbar.addPermanentWidget(self.ui.right_label)
        self.ui.right_label.setText('(0,0)')
        self.LeftButtonPressMove = False
        self.RightButtonPressMove = False

        self.ui.label.setCursor(Qt.CrossCursor)
        self.ui.pushButton.clicked.connect(self.ImageListSlect)
        self.ui.listWidget.currentRowChanged.connect(self.ListRowChanged)
        self.ui.pushButton_5.clicked.connect(self.StatsVideoShow)
        self.ui.pushButton_6.clicked.connect(self.StatsPerspectiveShow)

    def ImageListSlect(self):
        self.FileDirectory = QFileDialog.getExistingDirectory(self.ui, "选择图片列表", './Data/image')
        if self.FileDirectory != '':
            self.ui.listWidget.clear()
            self.ImagesPath = []
            for x in ('*.png', '*.jpg'):
                self.ImagesPath.extend(glob.glob(join(self.FileDirectory, x)))
            self.ImagesPath = sorted(self.ImagesPath, key=lambda name: int(name.split('.')[0].split('\\')[-1]))
            self.ImagesName = [os.path.basename(x) for x in self.ImagesPath]
            self.ui.left_label.setText('载入' + str(len(self.ImagesName)) + '张图片')
            self.ui.listWidget.addItems(self.ImagesName)
            self.ui.pushButton_2.clicked.connect(self.ImageListRefresh)
            self.ui.pushButton_3.clicked.connect(self.BeforeCurrentImage)
            self.ui.pushButton_4.clicked.connect(self.AfterCurrentImage)
        else:
            self.ui.left_label.setText('载入失败')

    def ImageListRefresh(self):
        self.ui.listWidget.clear()
        self.ImagesPath = []
        for x in ('*.png', '*.jpg'):
            self.ImagesPath.extend(glob.glob(join(self.FileDirectory, x)))
        self.ImagesPath = sorted(self.ImagesPath, key=lambda name: int(name.split('.')[0].split('\\')[-1]))
        self.ImagesName = [os.path.basename(x) for x in self.ImagesPath]
        self.ui.left_label.setText('载入' + str(len(self.ImagesName)) + '张图片')
        self.ui.listWidget.addItems(self.ImagesName)

    def ListRowChanged(self):
        self.ListItemRow = self.ui.listWidget.currentRow()
        self.image = QImage()
        self.imageRecord = QImage()
        self.ImagesLoadPath = self.ImagesPath[self.ListItemRow]
        self.image.load(self.ImagesLoadPath)
        self.imageRecord.load(self.ImagesLoadPath)
        self.image = QPixmap.fromImage(self.image)
        self.imageRecord = QPixmap.fromImage(self.imageRecord)
        self.imageSize = self.image.size()
        if self.ui.checkBox.isChecked() == True:
            self.DrawRowLines()
            self.DrawColLines()
        self.imageZoom = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
        self.ui.label.setPixmap(self.imageZoom)
        self.ui.label.setMaximumWidth(self.imageZoom.width())
        self.ui.label.setMaximumHeight(self.imageZoom.height())
        self.ui.label.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
        self.ui.label.move(self.ui.groupBox.width() / 2 - self.ui.label.width()/2, self.ui.groupBox.height() / 2 - self.ui.label.height() / 2)
        self.ui.left_label.setText('打开' + self.ImagesName[self.ListItemRow] + '成功')
        self.ui.label.installEventFilter(self)
        self.ui.label.setMouseTracking(True)
        self.ui.checkBox.stateChanged.connect(self.ImageGrid)

    def BeforeCurrentImage(self):
        if self.ui.listWidget.currentRow() > 0:
            self.ui.listWidget.setCurrentRow(self.ui.listWidget.currentRow()-1)

    def AfterCurrentImage(self):
        if self.ui.listWidget.currentRow() < len(self.ImagesName)-1:
            self.ui.listWidget.setCurrentRow(self.ui.listWidget.currentRow()+1)

    def eventFilter(self, watched, event):
        if watched == self.ui.label:
            if event.type() == QEvent.MouseButtonPress:
                if event.button() == Qt.LeftButton:
                    self.PaintBrush(event)
                    if self.ui.checkBox.isChecked() == True:
                        self.DrawRowLines()
                        self.DrawColLines()
                        self.imageDraw = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageDraw)
                        self.ui.label.setMaximumWidth(self.imageDraw.width())
                        self.ui.label.setMaximumHeight(self.imageDraw.height())
                        self.ui.label.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                        self.ui.label.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                    else:
                        self.imageDraw = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageDraw)
                        self.ui.label.setMaximumWidth(self.imageDraw.width())
                        self.ui.label.setMaximumHeight(self.imageDraw.height())
                        self.ui.label.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                        self.ui.label.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                if event.button() == Qt.RightButton:
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                    self.ui.label.setMouseTracking(False)
                    self.RightButtonPressMove = True
                return True
            if event.type() == QEvent.MouseButtonRelease:
                if event.button() == Qt.LeftButton:
                    self.ui.label.setMouseTracking(True)
                    self.LeftButtonPressMove = False
                if event.button() == Qt.RightButton:
                    self.ui.label.setMouseTracking(True)
                    self.RightButtonPressMove = False
                return True
            if event.type() == QEvent.MouseMove:
                self.ui.right_label.setText('(' + str(int(event.x() / self.imageScale // 5)) + ',' + str(int(event.y() / self.imageScale // 5)) + ')')
                if self.LeftButtonPressMove == True:
                    self.PaintBrush(event)
                    if self.ui.checkBox.isChecked() == True:
                        self.DrawRowLines()
                        self.DrawColLines()
                        self.imageDraw = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageDraw)
                        self.ui.label.setMaximumWidth(self.imageDraw.width())
                        self.ui.label.setMaximumHeight(self.imageDraw.height())
                        self.ui.label.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                    else:
                        self.imageDraw = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageDraw)
                        self.ui.label.setMaximumWidth(self.imageDraw.width())
                        self.ui.label.setMaximumHeight(self.imageDraw.height())
                        self.ui.label.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                if self.RightButtonPressMove == True:
                    self.ui.label.move(self.ui.label.x() + event.globalPos().x() - self.moveX, self.ui.label.y() + event.globalPos().y() - self.moveY)
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                return True
            if event.type() == QEvent.Wheel:
                self.wheelAngle = event.angleDelta() / 8
                if self.wheelAngle.y() > 0 and self.imageScale < 2:
                    self.imageScale += 0.2
                    if self.ui.checkBox.isChecked() == True:
                        self.imageZoom = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageZoom)
                        self.ui.label.setMaximumWidth(self.imageZoom.width())
                        self.ui.label.setMaximumHeight(self.imageZoom.height())
                        self.ui.label.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label.move(self.ui.groupBox.width() / 2 - self.ui.label.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label.height() / 2)
                    else:
                        self.imageZoom = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageZoom)
                        self.ui.label.setMaximumWidth(self.imageZoom.width())
                        self.ui.label.setMaximumHeight(self.imageZoom.height())
                        self.ui.label.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label.move(self.ui.groupBox.width() / 2 - self.ui.label.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label.height() / 2)
                if self.wheelAngle.y() < 0 and self.imageScale > 0.5:
                    self.imageScale -= 0.2
                    if self.ui.checkBox.isChecked() == True:
                        self.imageZoom = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageZoom)
                        self.ui.label.setMaximumWidth(self.imageZoom.width())
                        self.ui.label.setMaximumHeight(self.imageZoom.height())
                        self.ui.label.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label.move(self.ui.groupBox.width() / 2 - self.ui.label.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label.height() / 2)
                    else:
                        self.imageZoom = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label.setPixmap(self.imageZoom)
                        self.ui.label.setMaximumWidth(self.imageZoom.width())
                        self.ui.label.setMaximumHeight(self.imageZoom.height())
                        self.ui.label.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label.move(self.ui.groupBox.width() / 2 - self.ui.label.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label.height() / 2)
                return True
            return super(StatsChild, self).eventFilter(watched, event)

    def DrawRowLines(self):
        painter = QPainter(self.image)
        self.gap = 5
        self.lineY = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(0.0, self.lineY), QPointF(self.image.width(), self.lineY))
            self.lineY += self.gap
            if self.lineY > self.image.height():
                break

    def DrawColLines(self):
        painter = QPainter(self.image)
        self.gap = 5
        self.lineX = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(self.lineX, 0.0), QPointF(self.lineX, self.image.height()))
            self.lineX += self.gap
            if self.lineX > self.image.width():
                break

    def ImageGrid(self):
        if self.ui.checkBox.isChecked() == True:
            self.DrawRowLines()
            self.DrawColLines()
            self.imagePixelate = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
            self.ui.label.setPixmap(self.imagePixelate)
            self.ui.label.setMaximumWidth(self.imagePixelate.width())
            self.ui.label.setMaximumHeight(self.imagePixelate.height())
            self.ui.label.resize(QSize(self.imagePixelate.width(), self.imagePixelate.height()))
        else:
            self.imagePixelate = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
            self.ui.label.setPixmap(self.imagePixelate)
            self.ui.label.setMaximumWidth(self.imagePixelate.width())
            self.ui.label.setMaximumHeight(self.imagePixelate.height())
            self.ui.label.resize(QSize(self.imagePixelate.width(), self.imagePixelate.height()))

    def PaintBrush(self, event):
        painter = QPainter(self.image)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.imageScale//5*5 + n, event.y()/self.imageScale//5*5 + m))
        painter = QPainter(self.imageRecord)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.imageScale//5*5 + n, event.y()/self.imageScale//5*5 + m))

    def StatsVideoShow(self):
        self.StatsVideo=StatsVideo()
        self.StatsVideo.ui.show()

    def StatsPerspectiveShow(self):
        self.StatsPerspective=StatsPerspective()
        self.StatsPerspective.ui.show()

class StatsVideo():
    def __init__(self):
        self.ui = QUiLoader().load('untitledVideo.ui')
        self.ui.label_2.setText('未选择文件')
        self.ui.label_3.setText('未选择文件')
        self.text = self.ui.lineEdit.text()

        self.ui.pushButton_3.clicked.connect(self.ImageFileSelect)

    def ImageFileSelect(self):
        self.FileDirectory = QFileDialog.getExistingDirectory(self.ui, "选择图片列表", './Data/image')
        if self.FileDirectory != '':
            self.ImagesPath = []
            for x in ('*.png', '*.jpg'):
                self.ImagesPath.extend(glob.glob(join(self.FileDirectory, x)))
            self.ImagesPath = sorted(self.ImagesPath, key=lambda name: int(name.split('.')[0].split('\\')[-1]))
            self.ImagesName = ['./Data/image/' + os.path.basename(self.FileDirectory) + '/' + os.path.basename(x) for x in self.ImagesPath]
            self.ui.label_2.setText('已载入' + str(len(self.ImagesName)) + '张图片')
            self.ui.label_3.setText('预计时长' + str(len(self.ImagesName) / int(self.text)) + '秒')
            self.ui.lineEdit.textChanged.connect(self.LineEditChanged)
            self.ui.pushButton.clicked.connect(self.VideoGenerate)

    def LineEditChanged(self):
        self.text = self.ui.lineEdit.text()
        if self.text != '':
            self.ui.label_3.setText('预计时长' + str(len(self.ImagesName) / int(self.text)) + '秒')

    def VideoGenerate(self):
        i = datetime.datetime.now()
        filename = i.strftime("%Y-%m-%d %H-%M-%S")
        filename = filename + '.mp4'
        img = cv2.imread(self.ImagesName[1])
        video = cv2.VideoWriter('./Data/video/' + filename, cv2.VideoWriter_fourcc(*'mp4v'), int(self.text), (img.shape[1], img.shape[0]))
        self.ui.progressBar.setRange(0, len(self.ImagesName)-1)
        for i in range(1, len(self.ImagesName)):
            img = cv2.imread(self.ImagesName[i-1])
            video.write(img)
            self.ui.progressBar.setValue(i)
        video.release()

class StatsPerspective(QWidget):
    def __init__(self):
        super(StatsPerspective, self).__init__()
        self.ui = QUiLoader().load('untitledPerspective.ui')

        self.ui.left_label = QLabel()
        self.ui.right_label = QLabel()
        self.ui.statusbar.addWidget(self.ui.left_label)
        self.ui.statusbar.addPermanentWidget(self.ui.right_label)
        self.ui.right_label.setText('(0,0)')
        self.LeftButtonPressMove = False
        self.RightButtonPressMove = False

        self.ui.label_17.setCursor(Qt.CrossCursor)
        self.ui.label_18.setCursor(Qt.CrossCursor)
        self.ui.pushButton_3.clicked.connect(self.ImageListSlect)
        self.ui.listWidget.currentRowChanged.connect(self.ListRowChanged)

    def ImageListSlect(self):
        self.FileDirectory = QFileDialog.getExistingDirectory(self.ui, "选择图片列表", './Data/image')
        if self.FileDirectory != '':
            self.ui.listWidget.clear()
            self.ImagesPath = []
            for x in ('*.png', '*.jpg'):
                self.ImagesPath.extend(glob.glob(join(self.FileDirectory, x)))
            self.ImagesPath = sorted(self.ImagesPath, key=lambda name: int(name.split('.')[0].split('\\')[-1]))
            self.TransformImagesPath = ['./Data/image/' + os.path.basename(self.FileDirectory) + '/' + os.path.basename(x) for x in self.ImagesPath]
            self.ImagesName = [os.path.basename(x) for x in self.ImagesPath]
            self.ui.left_label.setText('载入' + str(len(self.ImagesName)) + '张图片')
            self.ui.listWidget.addItems(self.ImagesName)
        else:
            self.ui.left_label.setText('载入失败')

    def ListRowChanged(self):
        self.imageScale = 1
        self.ListItemRow = self.ui.listWidget.currentRow()
        self.image = QImage()
        self.imageRecord = QImage()
        self.ImagesLoadPath = self.ImagesPath[self.ListItemRow]
        self.image.load(self.ImagesLoadPath)
        self.imageRecord.load(self.ImagesLoadPath)
        self.image = QPixmap.fromImage(self.image)
        self.imageRecord = QPixmap.fromImage(self.imageRecord)
        self.imageSize = self.image.size()
        if self.ui.checkBox.isChecked() == True:
            self.DrawRowLines()
            self.DrawColLines()
        self.ui.label_17.setPixmap(self.image)
        self.ui.label_17.setMaximumWidth(self.image.width())
        self.ui.label_17.setMaximumHeight(self.image.height())
        self.ui.label_17.resize(QSize(self.image.width(), self.image.height()))
        self.ui.label_17.move(self.ui.groupBox.width() / 2 - self.ui.label_17.width()/2, self.ui.groupBox.height() / 2 - self.ui.label_17.height() / 2)
        self.ui.left_label.setText('打开' + self.ImagesName[self.ListItemRow] + '成功')
        self.ui.label_17.installEventFilter(self)
        self.ui.label_17.setMouseTracking(True)
        self.ui.checkBox.stateChanged.connect(self.ImageGrid)
        self.ui.pushButton.clicked.connect(self.PerspectiveTransform)

    def eventFilter(self, watched, event):
        if watched == self.ui.label_17:
            if event.type() == QEvent.MouseButtonPress:
                if event.button() == Qt.LeftButton:
                    self.PaintBrush(event)
                    if self.ui.checkBox.isChecked() == True:
                        self.DrawRowLines()
                        self.DrawColLines()
                        self.imageDraw = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageDraw)
                        self.ui.label_17.setMaximumWidth(self.imageDraw.width())
                        self.ui.label_17.setMaximumHeight(self.imageDraw.height())
                        self.ui.label_17.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                        self.ui.label_17.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                    else:
                        self.imageDraw = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageDraw)
                        self.ui.label_17.setMaximumWidth(self.imageDraw.width())
                        self.ui.label_17.setMaximumHeight(self.imageDraw.height())
                        self.ui.label_17.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                        self.ui.label_17.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                if event.button() == Qt.RightButton:
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                    self.ui.label_17.setMouseTracking(False)
                    self.RightButtonPressMove = True
                return True
            if event.type() == QEvent.MouseButtonRelease:
                if event.button() == Qt.LeftButton:
                    self.ui.label_17.setMouseTracking(True)
                    self.LeftButtonPressMove = False
                if event.button() == Qt.RightButton:
                    self.ui.label_17.setMouseTracking(True)
                    self.RightButtonPressMove = False
                return True
            if event.type() == QEvent.MouseMove:
                self.ui.right_label.setText('(' + str(int(event.x() / self.imageScale // 5)) + ',' + str(int(event.y() / self.imageScale // 5)) + ')')
                if self.LeftButtonPressMove == True:
                    self.PaintBrush(event)
                    if self.ui.checkBox.isChecked() == True:
                        self.DrawRowLines()
                        self.DrawColLines()
                        self.imageDraw = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageDraw)
                        self.ui.label_17.setMaximumWidth(self.imageDraw.width())
                        self.ui.label_17.setMaximumHeight(self.imageDraw.height())
                        self.ui.label_17.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                    else:
                        self.imageDraw = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageDraw)
                        self.ui.label_17.setMaximumWidth(self.imageDraw.width())
                        self.ui.label_17.setMaximumHeight(self.imageDraw.height())
                        self.ui.label_17.resize(QSize(self.imageDraw.width(), self.imageDraw.height()))
                if self.RightButtonPressMove == True:
                    self.ui.label_17.move(self.ui.label_17.x() + event.globalPos().x() - self.moveX, self.ui.label_17.y() + event.globalPos().y() - self.moveY)
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                return True
            if event.type() == QEvent.Wheel:
                self.wheelAngle = event.angleDelta() / 8
                if self.wheelAngle.y() > 0 and self.imageScale < 2:
                    self.imageScale += 0.2
                    if self.ui.checkBox.isChecked() == True:
                        self.imageZoom = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageZoom)
                        self.ui.label_17.setMaximumWidth(self.imageZoom.width())
                        self.ui.label_17.setMaximumHeight(self.imageZoom.height())
                        self.ui.label_17.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label_17.move(self.ui.groupBox.width() / 2 - self.ui.label_17.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label_17.height() / 2)
                    else:
                        self.imageZoom = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageZoom)
                        self.ui.label_17.setMaximumWidth(self.imageZoom.width())
                        self.ui.label_17.setMaximumHeight(self.imageZoom.height())
                        self.ui.label_17.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label_17.move(self.ui.groupBox.width() / 2 - self.ui.label_17.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label_17.height() / 2)
                if self.wheelAngle.y() < 0 and self.imageScale > 0.5:
                    self.imageScale -= 0.2
                    if self.ui.checkBox.isChecked() == True:
                        self.imageZoom = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageZoom)
                        self.ui.label_17.setMaximumWidth(self.imageZoom.width())
                        self.ui.label_17.setMaximumHeight(self.imageZoom.height())
                        self.ui.label_17.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label_17.move(self.ui.groupBox.width() / 2 - self.ui.label_17.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label_17.height() / 2)
                    else:
                        self.imageZoom = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_17.setPixmap(self.imageZoom)
                        self.ui.label_17.setMaximumWidth(self.imageZoom.width())
                        self.ui.label_17.setMaximumHeight(self.imageZoom.height())
                        self.ui.label_17.resize(QSize(self.imageZoom.width(), self.imageZoom.height()))
                        self.ui.label_17.move(self.ui.groupBox.width() / 2 - self.ui.label_17.width() / 2,
                                           self.ui.groupBox.height() / 2 - self.ui.label_17.height() / 2)
                return True
            return super(StatsPerspective, self).eventFilter(watched, event)
        elif watched == self.ui.label_18:
            if event.type() == QEvent.MouseButtonPress:
                if event.button() == Qt.LeftButton:
                    self.TransformPaintBrush(event)
                    if self.ui.checkBox_2.isChecked() == True:
                        self.TransformDrawRowLines()
                        self.TransformDrawColLines()
                        self.TransformImageDraw = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageDraw)
                        self.ui.label_18.setMaximumWidth(self.TransformImageDraw.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageDraw.height())
                        self.ui.label_18.resize(QSize(self.TransformImageDraw.width(), self.TransformImageDraw.height()))
                        self.ui.label_18.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                    else:
                        self.TransformImageDraw = self.TransformImageRecord.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageDraw)
                        self.ui.label_18.setMaximumWidth(self.TransformImageDraw.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageDraw.height())
                        self.ui.label_18.resize(QSize(self.TransformImageDraw.width(), self.TransformImageDraw.height()))
                        self.ui.label_18.setMouseTracking(False)
                        self.LeftButtonPressMove = True
                if event.button() == Qt.RightButton:
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                    self.ui.label_18.setMouseTracking(False)
                    self.RightButtonPressMove = True
                return True
            if event.type() == QEvent.MouseButtonRelease:
                if event.button() == Qt.LeftButton:
                    self.ui.label_18.setMouseTracking(True)
                    self.LeftButtonPressMove = False
                if event.button() == Qt.RightButton:
                    self.ui.label_18.setMouseTracking(True)
                    self.RightButtonPressMove = False
                return True
            if event.type() == QEvent.MouseMove:
                self.ui.right_label.setText('(' + str(int(event.x() / self.TransformImageScale // 5)) + ',' + str(
                    int(event.y() / self.TransformImageScale // 5)) + ')')
                if self.LeftButtonPressMove == True:
                    self.TransformPaintBrush(event)
                    if self.ui.checkBox_2.isChecked() == True:
                        self.TransformDrawRowLines()
                        self.TransformDrawColLines()
                        self.TransformImageDraw = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageDraw)
                        self.ui.label_18.setMaximumWidth(self.TransformImageDraw.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageDraw.height())
                        self.ui.label_18.resize(QSize(self.TransformImageDraw.width(), self.TransformImageDraw.height()))
                    else:
                        self.TransformImageDraw = self.TransformImageRecord.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageDraw)
                        self.ui.label_18.setMaximumWidth(self.TransformImageDraw.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageDraw.height())
                        self.ui.label_18.resize(QSize(self.TransformImageDraw.width(), self.TransformImageDraw.height()))
                if self.RightButtonPressMove == True:
                    self.ui.label_18.move(self.ui.label_18.x() + event.globalPos().x() - self.moveX,
                                          self.ui.label_18.y() + event.globalPos().y() - self.moveY)
                    self.moveX = event.globalPos().x()
                    self.moveY = event.globalPos().y()
                return True
            if event.type() == QEvent.Wheel:
                self.wheelAngle = event.angleDelta() / 8
                if self.wheelAngle.y() > 0 and self.TransformImageScale < 2:
                    self.TransformImageScale += 0.2
                    if self.ui.checkBox_2.isChecked() == True:
                        self.TransformImageZoom = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageZoom)
                        self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
                        self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
                        self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                              self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
                    else:
                        self.TransformImageZoom = self.TransformImageRecord.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageZoom)
                        self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
                        self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
                        self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                              self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
                if self.wheelAngle.y() < 0 and self.TransformImageScale > 0.5:
                    self.TransformImageScale -= 0.2
                    if self.ui.checkBox_2.isChecked() == True:
                        self.TransformImageZoom = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageZoom)
                        self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
                        self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
                        self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                              self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
                    else:
                        self.TransformImageZoom = self.TransformImageRecord.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
                        self.ui.label_18.setPixmap(self.TransformImageZoom)
                        self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
                        self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
                        self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
                        self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                              self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
                return True
            return super(StatsPerspective, self).eventFilter(watched, event)

    def DrawRowLines(self):
        painter = QPainter(self.image)
        self.gap = 5
        self.lineY = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(0.0, self.lineY), QPointF(self.image.width(), self.lineY))
            self.lineY += self.gap
            if self.lineY > self.image.height():
                break

    def DrawColLines(self):
        painter = QPainter(self.image)
        self.gap = 5
        self.lineX = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(self.lineX, 0.0), QPointF(self.lineX, self.image.height()))
            self.lineX += self.gap
            if self.lineX > self.image.width():
                break

    def ImageGrid(self):
        if self.ui.checkBox.isChecked() == True:
            self.DrawRowLines()
            self.DrawColLines()
            self.imagePixelate = self.image.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
            self.ui.label_17.setPixmap(self.imagePixelate)
            self.ui.label_17.setMaximumWidth(self.imagePixelate.width())
            self.ui.label_17.setMaximumHeight(self.imagePixelate.height())
            self.ui.label_17.resize(QSize(self.imagePixelate.width(), self.imagePixelate.height()))
        else:
            self.imagePixelate = self.imageRecord.scaled(self.imageSize * self.imageScale, Qt.IgnoreAspectRatio)
            self.ui.label_17.setPixmap(self.imagePixelate)
            self.ui.label_17.setMaximumWidth(self.imagePixelate.width())
            self.ui.label_17.setMaximumHeight(self.imagePixelate.height())
            self.ui.label_17.resize(QSize(self.imagePixelate.width(), self.imagePixelate.height()))

    def PaintBrush(self, event):
        painter = QPainter(self.image)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.imageScale//5*5 + n, event.y()/self.imageScale//5*5 + m))
        painter = QPainter(self.imageRecord)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.imageScale//5*5 + n, event.y()/self.imageScale//5*5 + m))

    def PerspectiveTransform(self):
        if self.ui.lineEdit_18.text() != '' and self.ui.lineEdit_17.text() != ''\
                and self.ui.lineEdit.text() != '' and self.ui.lineEdit_2.text() != ''\
                and self.ui.lineEdit_3.text() != '' and self.ui.lineEdit_4.text() != ''\
                and self.ui.lineEdit_5.text() != '' and self.ui.lineEdit_6.text() != ''\
                and self.ui.lineEdit_7.text() != '' and self.ui.lineEdit_8.text() != ''\
                and self.ui.lineEdit_9.text() != '' and self.ui.lineEdit_10.text() != ''\
                and self.ui.lineEdit_11.text() != '' and self.ui.lineEdit_12.text() != ''\
                and self.ui.lineEdit_13.text() != '' and self.ui.lineEdit_14.text() != ''\
                and self.ui.lineEdit_15.text() != '' and self.ui.lineEdit_16.text() != '':
            width = int(self.ui.lineEdit_18.text())
            height = int(self.ui.lineEdit_17.text())

            imageX1 = float(self.ui.lineEdit.text())
            imageX2 = float(self.ui.lineEdit_3.text())
            imageX3 = float(self.ui.lineEdit_5.text())
            imageX4 = float(self.ui.lineEdit_7.text())

            imageY1 = float(self.ui.lineEdit_2.text())
            imageY2 = float(self.ui.lineEdit_4.text())
            imageY3 = float(self.ui.lineEdit_6.text())
            imageY4 = float(self.ui.lineEdit_8.text())

            worldX1 = float(self.ui.lineEdit_9.text())
            worldX2 = float(self.ui.lineEdit_11.text())
            worldX3 = float(self.ui.lineEdit_13.text())
            worldX4 = float(self.ui.lineEdit_15.text())

            worldY1 = float(self.ui.lineEdit_10.text())
            worldY2 = float(self.ui.lineEdit_12.text())
            worldY3 = float(self.ui.lineEdit_14.text())
            worldY4 = float(self.ui.lineEdit_16.text())

            image = cv2.imread(self.TransformImagesPath[self.ListItemRow])
            image = cv2.resize(image, (width, height))

            pts1 = numpy.float32([[imageX1, imageY1], [imageX2, imageY2], [imageX3, imageY3], [imageX4, imageY4]])
            pts2 = numpy.float32([[worldX1, worldY1], [worldX2, worldY2], [worldX3, worldY3], [worldX4, worldY4]])

            M = cv2.getPerspectiveTransform(pts1, pts2)
            N = numpy.linalg.inv(M)
            with open('./Data/Transform/warped.txt', 'w') as f:
                numpy.savetxt(f, M, fmt='%.18e')
                numpy.savetxt(f, N, fmt='%.18e')

            warped = cv2.warpPerspective(image, M, (width, height))
            warped = cv2.resize(warped, (width * 5, height * 5))
            cv2.imwrite('./Data/Transform/warped.jpg', warped)

            self.TransformImageScale = 1
            self.TransformImage = QImage()
            self.TransformImageRecord = QImage()
            self.TransformImage.load('./Data/Transform/warped.jpg')
            self.TransformImageRecord.load('./Data/Transform/warped.jpg')
            self.TransformImage = QPixmap.fromImage(self.TransformImage)
            self.TransformImageRecord = QPixmap.fromImage(self.TransformImageRecord)
            self.TransformImageSize = self.TransformImage.size()
            if self.ui.checkBox_2.isChecked() == True:
                self.TransformDrawRowLines()
                self.TransformDrawColLines()
            self.ui.label_18.setPixmap(self.TransformImage)
            self.ui.label_18.setMaximumWidth(self.TransformImage.width())
            self.ui.label_18.setMaximumHeight(self.TransformImage.height())
            self.ui.label_18.resize(QSize(self.TransformImage.width(), self.TransformImage.height()))
            self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                  self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
            self.ui.left_label.setText('透视变换成功')
            self.ui.label_18.installEventFilter(self)
            self.ui.label_18.setMouseTracking(True)
            self.ui.checkBox_2.stateChanged.connect(self.TransformImageGrid)
            self.ui.lineEdit.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_2.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_3.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_4.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_5.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_6.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_7.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_8.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_9.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_10.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_11.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_12.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_13.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_14.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_15.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_16.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_17.textChanged.connect(self.LineEditChanged)
            self.ui.lineEdit_18.textChanged.connect(self.LineEditChanged)
            self.ui.listWidget.currentRowChanged.connect(self.TransformListRowChanged)
        elif self.ui.lineEdit_18.text() == '' or self.ui.lineEdit_17.text() == '':
            self.ui.left_label.setText('未设置图片大小')
        else:
            self.ui.left_label.setText('未设置坐标')

    def TransformDrawRowLines(self):
        painter = QPainter(self.TransformImage)
        self.gap = 5
        self.lineY = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(0.0, self.lineY), QPointF(self.TransformImage.width(), self.lineY))
            self.lineY += self.gap
            if self.lineY > self.TransformImage.height():
                break

    def TransformDrawColLines(self):
        painter = QPainter(self.TransformImage)
        self.gap = 5
        self.lineX = self.gap
        painter.setPen(QPen(QColor(120, 120, 120)))
        while True:
            painter.drawLine(QPointF(self.lineX, 0.0), QPointF(self.lineX, self.TransformImage.height()))
            self.lineX += self.gap
            if self.lineX > self.TransformImage.width():
                break

    def TransformImageGrid(self):
        if self.ui.checkBox_2.isChecked() == True:
            self.TransformDrawRowLines()
            self.TransformDrawColLines()
            self.TransformImagePixelate = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
            self.ui.label_18.setPixmap(self.TransformImagePixelate)
            self.ui.label_18.setMaximumWidth(self.TransformImagePixelate.width())
            self.ui.label_18.setMaximumHeight(self.TransformImagePixelate.height())
            self.ui.label_18.resize(QSize(self.TransformImagePixelate.width(), self.TransformImagePixelate.height()))
        else:
            self.TransformImagePixelate = self.TransformImageRecord.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
            self.ui.label_18.setPixmap(self.TransformImagePixelate)
            self.ui.label_18.setMaximumWidth(self.TransformImagePixelate.width())
            self.ui.label_18.setMaximumHeight(self.TransformImagePixelate.height())
            self.ui.label_18.resize(QSize(self.TransformImagePixelate.width(), self.TransformImagePixelate.height()))

    def TransformPaintBrush(self, event):
        painter = QPainter(self.TransformImage)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.TransformImageScale//5*5 + n, event.y()/self.TransformImageScale//5*5 + m))
        painter = QPainter(self.TransformImageRecord)
        painter.setPen(QPen(QColor(200, 200, 200)))
        for n in range(0, 5):
            for m in range(0, 5):
                painter.drawPoint(QPointF(event.x()/self.TransformImageScale//5*5 + n, event.y()/self.TransformImageScale//5*5 + m))

    def LineEditChanged(self):
        if self.ui.lineEdit_18.text() != '' and self.ui.lineEdit_17.text() != ''\
                and self.ui.lineEdit.text() != '' and self.ui.lineEdit_2.text() != ''\
                and self.ui.lineEdit_3.text() != '' and self.ui.lineEdit_4.text() != ''\
                and self.ui.lineEdit_5.text() != '' and self.ui.lineEdit_6.text() != ''\
                and self.ui.lineEdit_7.text() != '' and self.ui.lineEdit_8.text() != ''\
                and self.ui.lineEdit_9.text() != '' and self.ui.lineEdit_10.text() != ''\
                and self.ui.lineEdit_11.text() != '' and self.ui.lineEdit_12.text() != ''\
                and self.ui.lineEdit_13.text() != '' and self.ui.lineEdit_14.text() != ''\
                and self.ui.lineEdit_15.text() != '' and self.ui.lineEdit_16.text() != '':
            width = int(self.ui.lineEdit_18.text())
            height = int(self.ui.lineEdit_17.text())

            imageX1 = float(self.ui.lineEdit.text())
            imageX2 = float(self.ui.lineEdit_3.text())
            imageX3 = float(self.ui.lineEdit_5.text())
            imageX4 = float(self.ui.lineEdit_7.text())

            imageY1 = float(self.ui.lineEdit_2.text())
            imageY2 = float(self.ui.lineEdit_4.text())
            imageY3 = float(self.ui.lineEdit_6.text())
            imageY4 = float(self.ui.lineEdit_8.text())

            worldX1 = float(self.ui.lineEdit_9.text())
            worldX2 = float(self.ui.lineEdit_11.text())
            worldX3 = float(self.ui.lineEdit_13.text())
            worldX4 = float(self.ui.lineEdit_15.text())

            worldY1 = float(self.ui.lineEdit_10.text())
            worldY2 = float(self.ui.lineEdit_12.text())
            worldY3 = float(self.ui.lineEdit_14.text())
            worldY4 = float(self.ui.lineEdit_16.text())

            image = cv2.imread(self.TransformImagesPath[self.ListItemRow])
            image = cv2.resize(image, (width, height))

            pts1 = numpy.float32([[imageX1, imageY1], [imageX2, imageY2], [imageX3, imageY3], [imageX4, imageY4]])
            pts2 = numpy.float32([[worldX1, worldY1], [worldX2, worldY2], [worldX3, worldY3], [worldX4, worldY4]])

            M = cv2.getPerspectiveTransform(pts1, pts2)
            N = numpy.linalg.inv(M)
            with open('./Data/Transform/warped.txt', 'w') as f:
                numpy.savetxt(f, M, fmt='%.18e')
                numpy.savetxt(f, N, fmt='%.18e')

            warped = cv2.warpPerspective(image, M, (width, height))
            warped = cv2.resize(warped, (width * 5, height * 5))
            cv2.imwrite('./Data/Transform/warped.jpg', warped)

            #self.TransformImageScale = 1
            self.TransformImage = QImage()
            self.TransformImageRecord = QImage()
            self.TransformImage.load('./Data/Transform/warped.jpg')
            self.TransformImageRecord.load('./Data/Transform/warped.jpg')
            self.TransformImage = QPixmap.fromImage(self.TransformImage)
            self.TransformImageRecord = QPixmap.fromImage(self.TransformImageRecord)
            self.TransformImageSize = self.TransformImage.size()
            if self.ui.checkBox_2.isChecked() == True:
                self.TransformDrawRowLines()
                self.TransformDrawColLines()
            self.TransformImageZoom = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
            self.ui.label_18.setPixmap(self.TransformImageZoom)
            self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
            self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
            self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
            self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                  self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
            self.ui.left_label.setText('透视变换成功')
            self.ui.label_18.installEventFilter(self)
            self.ui.label_18.setMouseTracking(True)
            self.ui.checkBox_2.stateChanged.connect(self.TransformImageGrid)
        elif self.ui.lineEdit_18.text() == '' or self.ui.lineEdit_17.text() == '':
            self.ui.left_label.setText('未设置图片大小')
        else:
            self.ui.left_label.setText('未设置坐标')

    def TransformListRowChanged(self):
        if self.ui.lineEdit_18.text() != '' and self.ui.lineEdit_17.text() != ''\
                and self.ui.lineEdit.text() != '' and self.ui.lineEdit_2.text() != ''\
                and self.ui.lineEdit_3.text() != '' and self.ui.lineEdit_4.text() != ''\
                and self.ui.lineEdit_5.text() != '' and self.ui.lineEdit_6.text() != ''\
                and self.ui.lineEdit_7.text() != '' and self.ui.lineEdit_8.text() != ''\
                and self.ui.lineEdit_9.text() != '' and self.ui.lineEdit_10.text() != ''\
                and self.ui.lineEdit_11.text() != '' and self.ui.lineEdit_12.text() != ''\
                and self.ui.lineEdit_13.text() != '' and self.ui.lineEdit_14.text() != ''\
                and self.ui.lineEdit_15.text() != '' and self.ui.lineEdit_16.text() != '':
            width = int(self.ui.lineEdit_18.text())
            height = int(self.ui.lineEdit_17.text())

            imageX1 = float(self.ui.lineEdit.text())
            imageX2 = float(self.ui.lineEdit_3.text())
            imageX3 = float(self.ui.lineEdit_5.text())
            imageX4 = float(self.ui.lineEdit_7.text())

            imageY1 = float(self.ui.lineEdit_2.text())
            imageY2 = float(self.ui.lineEdit_4.text())
            imageY3 = float(self.ui.lineEdit_6.text())
            imageY4 = float(self.ui.lineEdit_8.text())

            worldX1 = float(self.ui.lineEdit_9.text())
            worldX2 = float(self.ui.lineEdit_11.text())
            worldX3 = float(self.ui.lineEdit_13.text())
            worldX4 = float(self.ui.lineEdit_15.text())

            worldY1 = float(self.ui.lineEdit_10.text())
            worldY2 = float(self.ui.lineEdit_12.text())
            worldY3 = float(self.ui.lineEdit_14.text())
            worldY4 = float(self.ui.lineEdit_16.text())

            image = cv2.imread(self.TransformImagesPath[self.ListItemRow])
            image = cv2.resize(image, (width, height))

            pts1 = numpy.float32([[imageX1, imageY1], [imageX2, imageY2], [imageX3, imageY3], [imageX4, imageY4]])
            pts2 = numpy.float32([[worldX1, worldY1], [worldX2, worldY2], [worldX3, worldY3], [worldX4, worldY4]])

            M = cv2.getPerspectiveTransform(pts1, pts2)
            N = numpy.linalg.inv(M)
            with open('./Data/Transform/warped.txt', 'w') as f:
                numpy.savetxt(f, M, fmt='%.18e')
                numpy.savetxt(f, N, fmt='%.18e')

            warped = cv2.warpPerspective(image, M, (width, height))
            warped = cv2.resize(warped, (width * 5, height * 5))
            cv2.imwrite('./Data/Transform/warped.jpg', warped)

            self.TransformImageScale = 1
            self.TransformImage = QImage()
            self.TransformImageRecord = QImage()
            self.TransformImage.load('./Data/Transform/warped.jpg')
            self.TransformImageRecord.load('./Data/Transform/warped.jpg')
            self.TransformImage = QPixmap.fromImage(self.TransformImage)
            self.TransformImageRecord = QPixmap.fromImage(self.TransformImageRecord)
            self.TransformImageSize = self.TransformImage.size()
            if self.ui.checkBox_2.isChecked() == True:
                self.TransformDrawRowLines()
                self.TransformDrawColLines()
            self.TransformImageZoom = self.TransformImage.scaled(self.TransformImageSize * self.TransformImageScale, Qt.IgnoreAspectRatio)
            self.ui.label_18.setPixmap(self.TransformImageZoom)
            self.ui.label_18.setMaximumWidth(self.TransformImageZoom.width())
            self.ui.label_18.setMaximumHeight(self.TransformImageZoom.height())
            self.ui.label_18.resize(QSize(self.TransformImageZoom.width(), self.TransformImageZoom.height()))
            self.ui.label_18.move(self.ui.groupBox_2.width() / 2 - self.ui.label_18.width() / 2,
                                  self.ui.groupBox_2.height() / 2 - self.ui.label_18.height() / 2)
            self.ui.left_label.setText('透视变换成功')
            self.ui.label_18.installEventFilter(self)
            self.ui.label_18.setMouseTracking(True)
            self.ui.checkBox_2.stateChanged.connect(self.TransformImageGrid)
        elif self.ui.lineEdit_18.text() == '' or self.ui.lineEdit_17.text() == '':
            self.ui.left_label.setText('未设置图片大小')
        else:
            self.ui.left_label.setText('未设置坐标')

app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()